<template>
  <div class="search-result">
    <van-list
      @load="getResult"
      v-model="loading"
      :finished="finished"
      finished-text="大咩呦 么得数据辽哇~~~~~~~~"
      :error.sync="error"
      error-text="请求失败，点击重新加载"
    >
      <van-cell
        v-for="item in results"
        :key="item.art_id"
        :title="item.title"
      ></van-cell>
    </van-list>
  </div>
</template>

<script>
import { getResultAPI } from '@/api'
export default {
  data() {
    return {
      results: [],
      page: 1,
      loading: false,
      finished: false,
      error: false
    }
  },
  methods: {
    async getResult() {
      try {
        // 发送请求获取数据
        const { data } = await getResultAPI(this.$parent.keywords, this.page++)
        // 添加数据
        this.results.push(...data.data.results)
        // 判断是否还有数据
        if (data.data.results.length === 0) {
          this.finished = true
        }
      } catch (error) {
        this.error = true
      } finally {
        this.loading = false
      }
    }
  }
}
</script>

<style lang="less" scoped>
.search-result {
  height: calc(100vh - 108px);
  overflow: auto;
}
</style>
